<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

define('IN_ADMIN_COMPONENT_CONTROLLER', 'true');

class OrderStatusComponent
{
	function OrderStatusComponent()
	{
	}

	function action(&$params, $action)
	{
		if ($action == 'Update') 
			return $this->edit($params);
		else
			return $this->query($params);
	}

	function query(&$params) 
	{
		$handle = get_class($this);
		$languageId = (int)$params['languageId'];
		
		$sqlQuery = 'select orders_status_id, orders_status_name, public_flag, downloads_flag ' .
		'from ' . TABLE_ORDERS_STATUS . ' ' .
		'where language_id='. $languageId . ' ' .
		'order by orders_status_id';

		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		$i = 0;
		while($row = mysql_fetch_array($dataReturned))
		{
			$value{$handle}{$i}{'id'}= (int)$row['orders_status_id'];
			$value{$handle}{$i}{'name'}= $row['orders_status_name'];
			$value{$handle}{$i}{'public_flag'}= (boolean)$row['public_flag'];
			$value{$handle}{$i}{'downloads_flag'}= (boolean)$row['downloads_flag'];
			$i++;
		}

		return $value;
	}

	function edit(&$params) 
	{
		$languageId = (int)$params['languageId'];

		$sql = 'delete from ' . TABLE_ORDERS_STATUS . ' where language_id=' . $languageId;
		mysql_query($sql) or die(mysql_error());

		$i = 0;
		while (true)
		{
			$i++;
			if ($i > 100) break;

			if (!isset($params['orderStatusId-'.$i])) break;

			$orderStatusId = (int)$params['orderStatusId-'.$i];
			if ($orderStatusName < 0) continue;
			
			$orderStatusName = prepare_input($params['orderStatusName-'.$i]);
			if ($orderStatusName == null || strlen($orderStatusName) == 0) continue;
			
			$orderStatusPublicFlag = (boolean)$params['orderStatusPublicFlag-'.$i];
			$orderStatusDownloadsFlag = (boolean)$params['orderStatusDownloadsFlag-'.$i];

			$deleteRow = (boolean)$params['orderStatusDelete-'.$i];
			if ($deleteRow) continue;

			$sql_data_array = array();
			$sqlAction = 'insert';
			$where = '';
			$sql_data_array['language_id']=$languageId;
			$sql_data_array['orders_status_id']=$orderStatusId;
			$sql_data_array['orders_status_name'] = $orderStatusName;
			$sql_data_array['public_flag'] = $orderStatusPublicFlag;
			$sql_data_array['downloads_flag'] = $orderStatusDownloadsFlag;
				
			db_perform(TABLE_ORDERS_STATUS, $sql_data_array, $sqlAction, $where);
		}

		return $this->query($params);
	}
}
?>
